package com.yt.user.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import com.yt.user.models.service.IAccountInfoService;
import com.yt.user.models.bo.AccountInfoBO;
import com.yt.user.models.dto.AccountInfoDTO;

import java.util.List;

/**
 * 账号信息控制器
 *
 * @author yzz
 * @Date 2025-10-22 09:37:39
 */

@RestController
@RequestMapping("/api/accountInfo")
@Slf4j
@Api(value = "AccountInfoApiController",tags = "账号信息Api接口")
public class AccountInfoApiController {

    @DubboReference
    private IAccountInfoService accountInfoService;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "String")
    public Result<AccountInfoDTO> get(@RequestParam("id") String id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        AccountInfoDTO dto = accountInfoService.selectById(id);

        return new Result<>(dto);
    }
    
    @PostMapping("/getByPhone")
    @ApiOperation(value = "根据手机号查找", notes = "根据手机号查找账号信息")
    @ApiImplicitParam(name = "phone", value = "手机号", required = true, paramType = "query", dataType = "String")
    public Result<List<AccountInfoDTO>> getByPhone(@RequestParam("phone") String phone) {
        if (phone == null || phone.isEmpty()) {
            return new Result<>(CodeEnum.FAIL_100002.getCode(), "手机号不能为空");
        }
        
        Wrapper<AccountInfoDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("phone", phone);
        List<AccountInfoDTO> dtoList = accountInfoService.selectList(wrapper);
        
        return new Result<>(dtoList);
    }

    @ApiOperation(value = "账号信息列表",notes = "账号信息列表")
    @PostMapping("/queryPageList")
    public Result<Page<AccountInfoDTO>> queryPageList(AccountInfoBO bo){
        Wrapper<AccountInfoDTO> wrapper = new EntityWrapper<>();
        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<AccountInfoDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = accountInfoService.selectPage(page, wrapper);

        return new Result<>(page);
    }
}